package demo001;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class BaseUse {
    public static void main(String[] args) {

        //1、生成流
        System.out.println("1、生成流:begin=============:");
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println(filtered);
        System.out.println("1、生成流:end=============:");

        //2、forEach
        System.out.println();
        System.out.println("2、forEach:begin=============:");
        Random random2 = new Random();
        random2.ints().limit(10).forEach(System.out::println);
        System.out.println("2、forEach:end=============:");

        //3、filter：设置的条件过滤出元素
        System.out.println();
        System.out.println("3、filter:begin=============");
        List<String> strings3 = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        long count = strings3.stream().filter(string -> string.isEmpty()).count();
        System.out.println(count);
        System.out.println("3、filter:end=============");

        //4、limit：获取指定数量的流
        System.out.println();
        System.out.println("4、limit:begin=============");
        Random random4 = new Random();
        random4.ints().limit(10).forEach(System.out::println);
        System.out.println("4、limit:end=============");

        //5、sorted：对流进行排序
        System.out.println();
        System.out.println("5、sorted:begin=============");
        Random random5 = new Random();
        random5.ints().limit(10).sorted().forEach(System.out::println);
        System.out.println("5、sorted:end=============");

        //6、并行（parallel）程序
        System.out.println();
        System.out.println("6、parallel:begin=============");
        List<String> strings6 = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        long count6 = strings6.parallelStream().filter(string -> string.isEmpty()).count();
        System.out.println(count6);
        System.out.println("6、parallel:end=============");

        //7、Collectors:将流转换成集合和聚合元素,可用于返回列表或字符串
        System.out.println();
        System.out.println("7、Collectors:begin=============");
        List<String> strings7 = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        List<String> filtered7 = strings7.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println("筛选列表: " + filtered7);
        String mergedString = strings7.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
        System.out.println("合并字符串: " + mergedString);
        System.out.println("7、Collectors:end=============");
        
        //8、统计
        System.out.println();
        System.out.println("8、Collectors:begin=============");
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

        IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();

        System.out.println("列表中最大的数 : " + stats.getMax());
        System.out.println("列表中最小的数 : " + stats.getMin());
        System.out.println("所有数之和 : " + stats.getSum());
        System.out.println("平均数 : " + stats.getAverage());
        System.out.println("8、Collectors:end=============");
        
    }
}
